package com.hqyj.javaGraduation.account.controller;

import com.hqyj.javaGraduation.account.pojo.Result;
import com.hqyj.javaGraduation.account.pojo.User;
import com.hqyj.javaGraduation.account.service.UserService;
import com.hqyj.javaGraduation.hotel.account.dao.AccountUserRoleMapper;
import com.hqyj.javaGraduation.hotel.account.pojo.AccountUserRole;
import com.hqyj.javaGraduation.hotel.account.service.AccountUserService;
import com.hqyj.javaGraduation.utils.MD5;
import com.hqyj.javaGraduation.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.support.atomic.RedisAtomicInteger;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private AccountUserRoleMapper accountUserRoleMapper;

    @PostMapping("/loginUser")
    public HashMap<String,Object> userLogin(User user, HttpServletRequest request){
        int loginMaxCount = 5;
        HashMap<String,Object> map = new HashMap<>();
        String pwd1 = MD5.string2MD5(user.getUserPassword());
        user.setUserPassword(pwd1);
        String key = user.getUserName();
        User user1 = userService.userLogin(user);
        if (user1!=null){
            map.put("info",user1);
            System.out.println(user1.getUserId());
            loginMaxCount=5;
        }else{
            redisUtils.set(key,loginMaxCount);
            if (loginMaxCount==0){
                map.put("info","你的账户已锁定，请30秒后再试");
                return map;
            }
            map.put("info","登陆失败!你还可以登"+ loginMaxCount-- +"次");
        }
        return map;
    }

    @PostMapping("/registerUser")
    public HashMap<String,Object> userRegister(User user){
        HashMap<String,Object> map = new HashMap<>();
        String pwd=	MD5.string2MD5(user.getUserPassword());
        user.setUserPassword(pwd);
        int i = userService.registerUser(user);
        if(i==1){
            AccountUserRole accountUserRole = new AccountUserRole();
            accountUserRole.setUserId(user.getUserId());
            accountUserRole.setRoleId(3);
            accountUserRoleMapper.insert(accountUserRole);
            map.put("info","注册成功！");
        }else {
            map.put("info","注册失败!");
        }
        return map;
    }

    @PostMapping( "/emailCode")
    public HashMap<String,Object> emailCode(String mail, HttpServletRequest req){
        return userService.emailSend(mail,req);
    }
    // redis发送邮箱验证码
    @PostMapping("/redisSend")
    public HashMap<String,Object> redisSendEmail(String email) {
        return userService.emailRedis(email);
    }
    //redis邮箱登录
    @PostMapping("/redisLogin")
    public HashMap<String,Object> redisLogin(String code,String email){
        return userService.LoginEmailRedis(code,email);
    }

    @PostMapping("/updatePwd")
    public HashMap<String,Object> updatePwd(User user){
        String pwd = MD5.string2MD5(user.getUserPassword());
        user.setUserPassword(pwd);
        return userService.updatePwd(user);
    }
}
